import pygame

class Slider(object):

	def __init__(self, x, y, background, gui,  sug_func, sug_field, field_range, min_val = 0, is_int = False, is_percent = False):
		'''a slider that calls a function when it is moved'''
		self.min_val = min_val
		self.is_int = is_int
		self.is_percent = is_percent
		self.sug_func = sug_func
		self.sug_field = sug_field
		self.sug_field_initial = self.sug_field
		self.field_range = field_range
		self.gui = gui
		self.background = pygame.image.load(background)
		
		#geometry
		self.slider = pygame.image.load('slider.png')
		self.slider_width = self.slider.get_width()
		self.slider_height = self.slider.get_height()
		self.back_width = self.background.get_width()
		self.back_height = self.background.get_height()
		self.slider_x = x-self.slider_width/2+self.back_width/2
		self.slider_y = y-self.slider_height/2 + self.back_height/2

		self.slider_coords = [self.slider_x, self.slider_y, self.slider_x+self.slider_width, self.slider_y+self.slider_height]
		self.background_coords =  [x,y,x+self.back_width,y+self.back_height]
		
		self.gui.sliders.append(self)
		self.unclick_slider()
		self.draw_slider()
		
	
	def draw_text(self, x, y, value):
		'''draws the text associated with the slider
		next to the slider'''
		self.msg_object = pygame.font.Font('freesansbold.ttf', 20).render(str(value), False, (255,255,255))
		self.msg_rect = self.msg_object.get_rect()
		self.msg_rect.topleft = (x + self.back_width + 2, y)
		self.gui.window.blit(self.msg_object, self.msg_rect)
		
		
	def click_slider(self, x, y):
		'''grabs the slider'''
		if x >= self.slider_coords[0] and x <= self.slider_coords[2]:
			if y >= self.slider_coords[1] and y <= self.slider_coords[3]:
				self.gui.clicked[self] = True
	
	def unclick_slider(self):
		'''releases the slider'''
		self.gui.clicked[self] = False
	
	def move_slider(self, x, y):
		'''moves the slider along the base'''
		if not self.gui.clicked[self]:
			#get outta here
			return False
			
		width = self.slider_width
		if x < (self.background_coords[0] + width/2): x = self.background_coords[0] + width/2
		if x > (self.background_coords[2] - width/2): x = self.background_coords[2] - width/2

		self.slider_coords[0] = x - width/2
		self.slider_coords[2] = x + width/2

		offset = width/2 + self.slider_coords[0] - (self.background_coords[2] + self.background_coords[0])/2
		
		percent = round(offset / float(self.background_coords[2] - self.background_coords[0] - width) + 1/2.,4) - .0001
		new_val = (percent - .50) * (2*self.field_range) + self.sug_field_initial

		new_val = new_val if new_val >= self.min_val else self.min_val
		if self.is_percent:
			new_val = int(new_val * 100)/100.
		if self.is_int:
			new_val = int(new_val)
		self.sug_field = new_val
		self.sug_func(self.sug_field)
		self.draw_slider()
		return True

	def draw_slider(self):
		'''draws the slider, its background,
		and the text on the gui'''
		width_boost = 0
		x,y = self.background_coords[0], self.background_coords[1]
		text = self.sug_field
		if self.is_percent:
			text = str(int(self.sug_field * 100)) + "%s" %"%"
			width_boost = 20
		elif self.is_int:
			text = int(self.sug_field)
		else:
			text = round(self.sug_field,1)
		pygame.draw.rect(self.gui.window,self.gui.background_color,(x,self.slider_y, self.back_width, self.slider_height))            
		pygame.draw.rect(self.gui.window, self.gui.background_color, (x+self.back_width, y-self.slider_height/2 + self.back_height/2, 45 + width_boost, self.slider_height))             
		self.draw_text(x,y, str(text))
		self.gui.window.blit(self.slider, (self.slider_coords[0], self.slider_coords[1]))
		self.gui.window.blit(self.background, (x, y))
	

